package com.wh.influxdb.common.config;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.wh.influxdb.common.core.InfluxdbTemplate;
import com.wh.influxdb.common.core.convertor.BytesHexConvertor;
import com.wh.influxdb.common.core.convertor.LocalDateTimeNanosConvertor;
import com.wh.influxdb.common.utils.AppContextUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @Author: xu
 * @Date: 2025/1/15
 * @Description:
 **/
@Configuration
@EnableConfigurationProperties(InfluxdbProperties.class)
@ConditionalOnProperty(prefix = "spring.influxdb", value = "enable", matchIfMissing = true)
@Import({AppContextUtil.class, BytesHexConvertor.class, LocalDateTimeNanosConvertor.class})
public class InfluxdbAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public InfluxDBClient influxDBClient(InfluxdbProperties influxdbProperties) {
        System.out.println("token:"+influxdbProperties.getToken());
        return InfluxDBClientFactory.create(
                influxdbProperties.getUrl(),
                influxdbProperties.getToken().toCharArray(),
                influxdbProperties.getOrg(),
                influxdbProperties.getBucket()
        );
    }

    @Bean
    public InfluxdbTemplate influxdbTemplate(InfluxdbProperties influxdbProperties) {
        return new InfluxdbTemplate(influxdbProperties.getBucket());
    }
}
